package com.hpsk.utils;

import com.hpsk.common.CommonException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.mock.web.MockMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author huangwang2832511019
 */
public class CommonFileUtil {

    public static MultipartFile setMultipartFile(File file) {
        String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
        String contentType = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        try {
            return new MockMultipartFile(fileName, fileName, contentType, new FileInputStream(file));
        } catch (IOException e) {
            throw new CommonException(e.getMessage());
        }
    }

    public static MultipartFile setInputStream(String fileName,InputStream inputStream) {
        try {
            return new MockMultipartFile(fileName,inputStream);
        } catch (IOException e) {
            throw new CommonException(e.getMessage());
        }
    }
}
